import { Button, Popconfirm } from 'antd'
import React from 'react'

const PopconfirmBtn = (prop: any) => {
  const [confirmLoading, setConfirmLoading] = React.useState(false)
  const onConfirm = () => {
    return async () => {
      setConfirmLoading(true)
      try {
        await prop.onConfirm()
        setConfirmLoading(false)
      } catch (error) {
        setConfirmLoading(false)
      }
    }
  }
  return (
    <Popconfirm
      title="提示"
      description={`确定删除${prop.title}吗？`}
      okText="确定"
      cancelText="取消"
      onConfirm={ onConfirm }
      onCancel={() => prop.onCancel}
      okButtonProps={{ loading: confirmLoading }}
    >
      <Button type="text" danger>{ prop.children }</Button>
    </Popconfirm>
  )
}

export default PopconfirmBtn
